package com.platform.web.controller.data;

import com.platform.base.domain.BaseLocation;
import com.platform.base.service.IBaseLocationService;
import com.platform.common.annotation.Log;
import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.enums.BusinessType;
import com.platform.common.utils.poi.ExcelUtil;
import com.platform.common.utils.redis.RedisUtil;
import com.platform.framework.util.ShiroUtils;
import com.platform.data.domain.EqAlarm;
import com.platform.data.service.IEqAlarmService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备告警记录Controller
 * 
 * @author wangqi
 * @date 2020-08-14
 */
@Controller
@RequestMapping("/data/alarm")
public class EqAlarmController extends BaseController
{
    private String prefix = "data/alarm";

    @Autowired
    private IEqAlarmService eqAlarmService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private IBaseLocationService baseLocationService;

    @RequiresPermissions("data:alarm:view")
    @GetMapping()
    public String alarm( ModelMap mmap)
    {
        if(redisUtil.hasKey("error")) {
            redisUtil.del("error");
        }
        mmap.put("location", new BaseLocation());

        return prefix + "/alarm";
    }

    /**
     * 查询设备告警记录列表
     */
    @RequiresPermissions("data:alarm:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EqAlarm eqAlarm)
    {
        startPage();
        List<EqAlarm> list = eqAlarmService.selectEqAlarmList(eqAlarm);
        return getDataTable(list);
    }

    /**
     * 导出设备告警记录列表
     */
    @RequiresPermissions("data:alarm:export")
    @Log(title = "设备告警记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EqAlarm eqAlarm)
    {
        List<EqAlarm> list = eqAlarmService.selectEqAlarmList(eqAlarm);
        ExcelUtil<EqAlarm> util = new ExcelUtil<EqAlarm>(EqAlarm.class);
        return util.exportExcel(list, "alarm");
    }

    /**
     * 新增设备告警记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备告警记录
     */
    @RequiresPermissions("data:alarm:add")
    @Log(title = "设备告警记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EqAlarm eqAlarm)
    {
        eqAlarm.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(eqAlarmService.insertEqAlarm(eqAlarm));
    }

    /**
     * 修改设备告警记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        EqAlarm eqAlarm = eqAlarmService.getById(id);
        mmap.put("eqAlarm", eqAlarm);
        return prefix + "/edit";
    }



    /**
     * 修改保存设备告警记录
     */
    @RequiresPermissions("data:alarm:edit")
    @Log(title = "设备告警记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EqAlarm eqAlarm)
    {
        eqAlarm.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(eqAlarmService.updateEqAlarm(eqAlarm));
    }

    /**
     * 删除设备告警记录
     */
    @RequiresPermissions("data:alarm:remove")
    @Log(title = "设备告警记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(eqAlarmService.deleteEqAlarmByIds(ids));
    }
    /**
     * 用户状态修改
     */
    @Log(title = "设备告警管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("data:alarm:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(EqAlarm eqAlarm)
    {
        return toAjax(eqAlarmService.changeStatus(eqAlarm));
    }
}
